package com.example.donghuihui.androidframe.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.example.donghuihui.androidframe.BaseApplication;
import com.example.donghuihui.androidframe.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

/**
 * Created by zzg on 2016/1/19.
 * 图片加载器
 */
public class ImageLoaderUtil {
    private Context mContext;
    private static volatile ImageLoaderUtil mDownloader;
    private ImageLoaderConfiguration mConfiguration;
    private ImageLoaderConfiguration.Builder mConfigurationBuilder;
    private ImageLoader mImageLoader;

    public DisplayImageOptions mDefaultImageOptions;

    /**
     * 缓存目录
     */
    File mCacheDir = StorageUtils.getOwnCacheDirectory(BaseApplication.getInstance(), Constant.DEFAULT_SAVE_IMAGE_PATH);

    private ImageLoaderUtil(Context context, ImageLoaderConfiguration configuration) {
        mContext = context;
        initOptions(R.mipmap.ic_launcher);
        if (configuration == null) {
            mConfiguration = createDefaultConfiguration();
        } else {
            mConfiguration = configuration;
        }
        if (mImageLoader == null) {
            initImageLoader(mConfiguration);
        }
        if (mDefaultImageOptions == null) {
            mDefaultImageOptions = initOptions(R.mipmap.ic_launcher);
        }
    }

    public static synchronized ImageLoaderUtil getInstance(Context context) {
        if (mDownloader == null) {
            mDownloader = new ImageLoaderUtil(context, null);
        }
        return mDownloader;
    }

    public static synchronized ImageLoaderUtil getInstance(Context context, ImageLoaderConfiguration configuration) {
        if (mDownloader == null) {
            mDownloader = new ImageLoaderUtil(context, configuration);
        }
        return mDownloader;
    }

    private ImageLoaderConfiguration createDefaultConfiguration(){
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)
//                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCache(new WeakMemoryCache())
                .diskCacheSize(50 * 1024 * 1024)
                .diskCacheFileCount(100)
                .diskCache(new UnlimitedDiskCache(mCacheDir))
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .defaultDisplayImageOptions(mDefaultImageOptions).build();
        return config;
    }

    public DisplayImageOptions initOptions(int resId) {
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
        builder.cacheInMemory(true).cacheOnDisk(true)
//                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
//                .displayer(new FadeInBitmapDisplayer(300))
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageForEmptyUri(resId)
                .showImageOnFail(resId)
//                .displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角，弧度为多少(服务器没做图片压缩不建议使用，会创建新的ARGB_8888格式的Bitmap对象；)
                .displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间
//                .showImageOnLoading(resId)
//                .displayer(DefaultConfigurationFactory.createBitmapDisplayer())
        ;
        return builder.build();
    }

    /**
     * 通过Configuration.Builder重置ImageLoader
     *
     * @param builder
     */
    public void initImageLoader(ImageLoaderConfiguration.Builder builder) {
        if (null == builder) {
            return;
        }
        mConfigurationBuilder = builder;
        initImageLoader(builder.build());
    }

    /**
     * 通过Configuration重置ImageLoader
     *
     * @param configuration
     */
    public void initImageLoader(ImageLoaderConfiguration configuration) {
        if (null == configuration) {
            return;
        }
        mConfiguration = configuration;
        mImageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
        mImageLoader.init(configuration);
    }

    public void displayImage(String uri, ImageView imageView) {
        mImageLoader.displayImage(uri, imageView, mDefaultImageOptions);
    }

    public void displayImage(String uri, ImageView imageView, DisplayImageOptions options) {
        mImageLoader.displayImage(uri, imageView, options);
    }

    public void displayImage(String uri, ImageView imageView, ImageLoadingListener listener) {
        mImageLoader.displayImage(uri, imageView, mDefaultImageOptions, listener);
    }

    public void displayImage(String uri, ImageView imageView, DisplayImageOptions options, ImageLoadingListener listener) {
        mImageLoader.displayImage(uri, imageView, options, listener);
    }

    public void displayImage(String uri, ImageView imageView, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
        mImageLoader.displayImage(uri, imageView, mDefaultImageOptions, listener,progressListener);
    }

    public void displayImage(String uri, ImageView imageView, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
        mImageLoader.displayImage(uri, imageView, options, listener,progressListener);
    }

    public void loadImage(String uri, ImageLoadingListener listener) {
        mImageLoader.loadImage(uri, mDefaultImageOptions, listener);
    }

    public void loadImage(String uri, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
        mImageLoader.loadImage(uri, null, mDefaultImageOptions, listener, progressListener);
    }

    public void loadImage(String uri, DisplayImageOptions options, ImageLoadingListener listener) {
        mImageLoader.loadImage(uri, options, listener);
    }

    public void loadImage(String uri, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
        mImageLoader.loadImage(uri, null, options, listener, progressListener);
    }

    public void loadImage(String uri, ImageSize minImageSize, ImageLoadingListener listener) {
        mImageLoader.loadImage(uri, minImageSize, mDefaultImageOptions, listener);
    }

    public void loadImage(String uri, ImageSize minImageSize, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
        mImageLoader.loadImage(uri, minImageSize, mDefaultImageOptions, listener,progressListener);
    }

    public void loadImage(String uri, ImageSize targetImageSize, DisplayImageOptions options, ImageLoadingListener listener) {
        mImageLoader.loadImage(uri, targetImageSize, options, listener);
    }

    public void loadImage(String uri, ImageSize targetImageSize, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
        mImageLoader.loadImage(uri, targetImageSize, options, listener,progressListener);
    }

    public void onResume() {
        mImageLoader.resume();
    }

    public void onPause() {
        mImageLoader.pause();
    }

    public void onStop() {
        mImageLoader.stop();
    }

    public void onDestroy() {
        mImageLoader.destroy();
        mDownloader = null;
    }

    public void clearMemoryCache() {
        mImageLoader.clearMemoryCache();
    }

    public void clearDiscCache() {
        mImageLoader.clearDiskCache();
    }
}
